package cn.iocoder.yudao.module.shop.service.shopTable;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.shop.controller.admin.shopTable.vo.ShopTablePageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.shopTable.vo.ShopTableSaveReqVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopInfo.ShopInfoDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopTable.ShopTableDO;
import cn.iocoder.yudao.module.shop.dal.mysql.shopInfo.ShopInfoMapper;
import cn.iocoder.yudao.module.shop.dal.mysql.shopTable.ShopTableMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.SHOP_INFO_NOT_EXISTS;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.TABLE_NOT_EXISTS;

/**
 * 商户桌号 Service 实现类
 *
 * @author 吕小文
 */
@Service
@Validated
public class ShopTableServiceImpl implements ShopTableService {

    @Resource
    private ShopTableMapper tableMapper;
    @Resource
    private ShopInfoMapper shopInfoMapper;

    @Override
    public Long createTable(ShopTableSaveReqVO createReqVO) {
        // 插入
        ShopTableDO table = BeanUtils.toBean(createReqVO, ShopTableDO.class);
        tableMapper.insert(table);
        // 返回
        return table.getId();
    }

    @Override
    public void updateTable(ShopTableSaveReqVO updateReqVO) {
        // 校验存在
        validateTableExists(updateReqVO.getId());
        // 更新
        ShopTableDO updateObj = BeanUtils.toBean(updateReqVO, ShopTableDO.class);
        tableMapper.updateById(updateObj);
    }

    @Override
    public void deleteTable(Long id) {
        // 校验存在
        validateTableExists(id);
        // 删除
        tableMapper.deleteById(id);
    }

    private void validateTableExists(Long id) {
        if (tableMapper.selectById(id) == null) {
            throw exception(TABLE_NOT_EXISTS);
        }
    }

    @Override
    public ShopTableDO getTable(Long id) {
        return tableMapper.selectById(id);
    }

    @Override
    public PageResult<ShopTableDO> getTablePage(ShopTablePageReqVO pageReqVO) {
        return tableMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateTableBatch(ShopTableSaveReqVO.ShopTableUpdateReqVO vo) {
        //获取商户id
        Long shopId = vo.getShopId();
        ShopInfoDO shopInfoDO = shopInfoMapper.selectById(shopId);
        if (shopInfoDO == null){
            throw exception(SHOP_INFO_NOT_EXISTS);
        }


        //获取本身的桌号数据
        List<ShopTableDO> dbTableList = tableMapper.selectByShopId(shopId);



        Map<Long, ShopTableDO> dbTableMap = dbTableList.stream().collect(Collectors.toMap(ShopTableDO::getId, Function.identity(), (v1, v2) -> v1));


        //获取数据列表
        List<ShopTableDO> itemList = BeanUtils.toBean(vo.getItemList(),ShopTableDO.class);
        Map<Long, ShopTableDO> itemMap = itemList.stream().filter(item-> item.getId()!=null && item.getId() != 0L).collect(Collectors.toMap(ShopTableDO::getId, Function.identity(), (v1, v2) -> v1));


        List<ShopTableDO> insertList = new ArrayList<>();
        List<ShopTableDO> updateList = new ArrayList<>();
        List<Long> deleteList = new ArrayList<>();

        //获取删除的记录
        for (ShopTableDO item : dbTableList){
            ShopTableDO shopTableDO = itemMap.get(item.getId());
            if (shopTableDO == null){
                if ("01".equals(item.getSeatStatus())){
                    //如果占座中，跳过该记录
                   continue;
                }
                deleteList.add(item.getId());
            }
        }

        for (ShopTableDO item : itemList){
            //获取新增的记录
            if (item.getId() == null || item.getId() == 0L){
                item.setId(null);
                item.setShopId(shopId);
                item.setBrandId(shopInfoDO.getBrandId());
                item.setIsUse(true);
                item.setSeatStatus("01");
                insertList.add(item);
                continue;
            }

            //获取更新的记录
            ShopTableDO shopTableDO = dbTableMap.get(item.getId());
            if (shopTableDO!=null){
                item.setShopId(shopId);
                item.setBrandId(shopInfoDO.getBrandId());
                updateList.add(item);
            }
        }

        if (CollectionUtil.isNotEmpty(insertList)){
            tableMapper.insertBatch(insertList);
        }
        if (CollectionUtil.isNotEmpty(updateList)){
            tableMapper.updateBatch(updateList);
        }
        if (CollectionUtil.isNotEmpty(deleteList)){
            tableMapper.deleteBatchIds(deleteList);
        }
    }

}